﻿package plugin.component.view
{
    import com.sh.game.display.*;
    import com.sh.game.loader.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.utils.*;
    import morn.core.handlers.*;
    import plugin.component.view.enum.*;

    public class ActorMovie extends Object
    {
        private var _model:String;
        public var fps:int = 0;
        public var standX:int = 0;
        public var standY:int = 0;
        public var totalDir:int = 0;
        public var _mc:BMDMovieClip;
        public var _mctype:int = 1;
        private var arm:Object;
        private var _sourceName:int = 0;
        private var _disposed:Boolean = false;
        private var _action:int;
        private var _container:RoleShow;
        private var _ismonstor:Boolean;
        private const mdefultAction:int = 1;
        private var _readers:Dictionary;
        private var _updatefunc:Function;
        private var _playAction:Boolean = false;

        public function ActorMovie(param1:RoleShow, param2:String, param3:int, param4:Boolean = false)
        {
            this._readers = new Dictionary();
            this._ismonstor = param4;
            this._model = param2;
            this._mctype = param3;
            if (this._model == null)
            {
                return;
            }
            var _loc_5:* = this.initUrl();
            if (param4)
            {
                this._action = ActionType.ATTACK;
                this._playAction = true;
            }
            this._container = param1;
            this._sourceName = ModelName.getSource(param2, this._action, 4);
            SWFActionLoader2.instance.load(_loc_5, this.onComplete, this._model, this._action, true, 4);
            return;
        }// end function

        private function initUrl() : String
        {
            var _loc_1:String = null;
            if (this._mctype == McType.EffCloseth || this._mctype == McType.Effhand)
            {
                _loc_1 = "assets/images/effects/";
                this._action = this.mdefultAction;
            }
            else
            {
                this._action = ActionType.SHOWSTAND;
                if (this._ismonstor)
                {
                    _loc_1 = "assets/models/monster/";
                }
                else
                {
                    _loc_1 = "assets/models/show/";
                }
            }
            return _loc_1;
        }// end function

        private function onComplete(param1:ActionAssets, param2:int, param3:int) : void
        {
            var _loc_5:Array = null;
            var _loc_6:Dictionary = null;
            this._sourceName = param2;
            this._readers[this._sourceName] = param1;
            if (param1 == null)
            {
                return;
            }
            if (this._model == null)
            {
                SWFActionLoader2.instance.unload(this._sourceName, true);
                return;
            }
            if (this._container == null)
            {
                return;
            }
            var _loc_4:* = ConfigDictionary.data.models;
            if (ConfigDictionary.data.models[this._model] != null && _loc_4[this._model][this._action])
            {
                _loc_5 = _loc_4[this._model][this._action];
            }
            else
            {
                if (this._ismonstor)
                {
                    _loc_6 = SkinConfig.getMonster();
                }
                else
                {
                    _loc_6 = SkinConfig.getPlayer();
                }
                _loc_5 = _loc_6[this._action];
            }
            this.standX = _loc_5[2];
            this.standY = _loc_5[3];
            if (this._mc)
            {
                this._mc.dispose();
            }
            this._mc = null;
            this._mc = new BMDMovieClip(param1.bmps, param1.frameSet);
            this._container.addChild(this._mc);
            if (this._mctype == McType.EffCloseth || this._mctype == McType.Effhand)
            {
                this._mc.blendMode = BlendMode.ADD;
            }
            this._container.addMovie(this);
            this._mc.pivotX = this.standX;
            this._mc.pivotY = this.standY;
            this._mc.interval = _loc_5[1];
            if (this._playAction)
            {
                this._playAction = false;
                this._mc.playFromTo(-1, (this._mc.totalFrame - 1), new Handler(this.onCompletefun));
            }
            else
            {
                this._mc.play();
            }
            return;
        }// end function

        private function onCompletefun() : void
        {
            if (this._action != 1 && this._ismonstor)
            {
                this.doAction(this.mdefultAction);
            }
            return;
        }// end function

        public function updatemodel(param1:String) : void
        {
            if (this._model && this._model == param1)
            {
                return;
            }
            this.clear();
            this._disposed = false;
            this._model = param1;
            var _loc_2:* = this.initUrl();
            if (this._ismonstor)
            {
                this._action = ActionType.ATTACK;
                this._playAction = true;
            }
            this._sourceName = ModelName.getSource(param1, this._action, 4);
            if (this._readers[this._sourceName])
            {
                this.onComplete(this._readers[this._sourceName], this._sourceName, 4);
            }
            else
            {
                SWFActionLoader2.instance.load(_loc_2, this.onComplete, this._model, this._action, true, 4);
            }
            return;
        }// end function

        public function doAction(param1:int = 1) : void
        {
            if (this._action == param1 || !this._ismonstor)
            {
                return;
            }
            if (this._mc && this._mc.parent)
            {
                this._mc.parent.removeChild(this._mc);
            }
            if (this._mc)
            {
                this._mc.dispose();
            }
            this._mc = null;
            if (param1 != this.mdefultAction)
            {
                this._playAction = true;
            }
            var _loc_2:* = this._model;
            this._disposed = false;
            var _loc_3:* = this.initUrl();
            this._action = param1;
            this._model = _loc_2;
            this._sourceName = ModelName.getSource(this._model, this._action, 4);
            if (this._readers[this._sourceName])
            {
                this.onComplete(this._readers[this._sourceName], this._sourceName, 4);
            }
            else
            {
                SWFActionLoader2.instance.load(_loc_3, this.onComplete, this._model, this._action, true, 4);
            }
            return;
        }// end function

        public function UpdateIndex(param1:int) : void
        {
            if (this._mc && this._mc.parent)
            {
                this._mc.parent.setChildIndex(this._mc, param1);
            }
            return;
        }// end function

        public function restartMc() : void
        {
            if (this._mc)
            {
                this._mc.gotoAndPlay(0);
            }
            return;
        }// end function

        private function clear() : void
        {
            this._disposed = true;
            if (this._model == null)
            {
                return;
            }
            if (this._sourceName)
            {
                SWFActionLoader2.instance.unload(this._sourceName, true);
            }
            delete this._readers[ModelName.getSource(this._model, 1, 4)];
            delete this._readers[ModelName.getSource(this._model, 4, 4)];
            delete this._readers[ModelName.getSource(this._model, ActionType.SHOWSTAND, 4)];
            this._sourceName = 0;
            this._model = null;
            this._action = 0;
            this.fps = 0;
            this.standX = 0;
            this.standY = 0;
            this.totalDir = 0;
            if (this._mc && this._mc.parent)
            {
                this._mc.parent.removeChild(this._mc);
            }
            if (this._mc)
            {
                this._mc.dispose();
            }
            this._mc = null;
            return;
        }// end function

        public function dispose() : void
        {
            var _loc_1:String = null;
            if (this._disposed)
            {
                return;
            }
            this._disposed = true;
            if (this._model == null)
            {
                return;
            }
            if (this._sourceName)
            {
                SWFActionLoader2.instance.unload(this._sourceName, true);
            }
            this._sourceName = 0;
            this._model = null;
            this._action = 0;
            this.fps = 0;
            this.standX = 0;
            this.standY = 0;
            for (_loc_1 in this._readers)
            {
                
                this._readers[_loc_1] = null;
                delete this._readers[_loc_1];
            }
            this.totalDir = 0;
            if (this._mc && this._mc.parent)
            {
                this._mc.parent.removeChild(this._mc);
            }
            if (this._mc)
            {
                this._mc.dispose();
            }
            this._mc = null;
            return;
        }// end function

    }
}
